f9ad86
@@ -1,5 +1,5 @@
 /*
- * Copyright 2002-2015 the original author or authors.
+ * Copyright 2002-2016 the original author or authors.
  *
  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  * the License. You may obtain a copy of the License at
@@ -17,6 +17,7 @@
import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.LinkedList;
+import java.util.List;
 import java.util.Properties;
 import java.util.concurrent.TimeoutException;
 import java.util.concurrent.atomic.AtomicBoolean;
@@ -159,6 +160,10 @@
public class RabbitAdmin implements AmqpAdmin, ApplicationContextAware, Initiali
 	 * If the queue doesn't have a value for 'name' property,
 	 * the queue name will be generated by Broker and returned from this method.
 	 * But the 'name' property of the queue remains as is.
+	 * @param queue the queue
+	 * @return the queue name if successful, null if not successful and
+	 * {@link #setIgnoreDeclarationExceptions(boolean) ignoreDeclarationExceptions} is
+	 * true.
 	 */
 	@Override
 	@ManagedOperation
@@ -166,7 +171,8 @@
public class RabbitAdmin implements AmqpAdmin, ApplicationContextAware, Initiali
 		return this.rabbitTemplate.execute(new ChannelCallback<String>() {
 			@Override
 			public String doInRabbit(Channel channel) throws Exception {
-				return declareQueues(channel, queue)[0].getQueue();
+				DeclareOk[] declared = declareQueues(channel, queue);
+				return declared.length > 0 ? declared[0].getQueue() : null;
 			}
 		});
 	}
@@ -487,7 +493,7 @@
public class RabbitAdmin implements AmqpAdmin, ApplicationContextAware, Initiali
 	}
 
 	private DeclareOk[] declareQueues(final Channel channel, final Queue... queues) throws IOException {
-		DeclareOk[] declareOks = new DeclareOk[queues.length];
+		List<DeclareOk> declareOks = new ArrayList<DeclareOk>(queues.length);
 		for (int i = 0; i < queues.length; i++) {
 			Queue queue = queues[i];
 			if (!queue.getName().startsWith("amq.")) {
@@ -498,7 +504,7 @@
public class RabbitAdmin implements AmqpAdmin, ApplicationContextAware, Initiali
 					try {
 						DeclareOk declareOk = channel.queueDeclare(queue.getName(), queue.isDurable(), queue.isExclusive(), queue.isAutoDelete(),
 								queue.getArguments());
-						declareOks[i] = declareOk;
+						declareOks.add(declareOk);
 					}
 					catch (IllegalArgumentException e) {
 						try {
@@ -525,7 +531,7 @@
public class RabbitAdmin implements AmqpAdmin, ApplicationContextAware, Initiali
 				logger.debug("Queue with name that starts with 'amq.' cannot be declared.");
 			}
 		}
-		return declareOks;
+		return declareOks.toArray(new DeclareOk[declareOks.size()]);
 	}
 
 	private void declareBindings(final Channel channel, final Binding... bindings) throws IOException {
